package com.qiankan.qmsj.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.qiankan.qmsj.R;
import com.qiankan.qmsj.entity.Result;
import com.qiankan.qmsj.net.APIHub;
import com.qiankan.qmsj.utils.CountDownTimerUtils;
import com.qiankan.qmsj.utils.StringUtils;
import com.qiankan.qmsj.widget.TipToast;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ForgetPasswordActivity extends BaseActivity {
    @BindView(R.id.et_phoneNum)
    EditText mEtPhoneNum;
    @BindView(R.id.et_password)
    EditText mEtPassword;
    @BindView(R.id.et_verityCode)
    EditText mEtVerityCode;
    @BindView(R.id.tv_verify)
    TextView mVerify;

    private CountDownTimerUtils mCountDownTimerUtils;

    public static Intent newIntent(Context context) {
        return new Intent(context, ForgetPasswordActivity.class);
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_forget_password;
    }

    @Override
    protected void initAllMembersView(Bundle savedInstanceState) {
        mCountDownTimerUtils = new CountDownTimerUtils(mVerify, 60000, 1000);
    }

    @OnClick(R.id.tv_verify)
    public void onClickVerify(View view) {
        String phone = mEtPhoneNum.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            TipToast.toast(this, "请输入手机号");
            return;
        }
        if (!StringUtils.isPhone(phone)) {
            TipToast.toast(this, "请输入正确的手机号");
            return;
        }
        mCountDownTimerUtils.start();
        Map<String, String> param = new HashMap<String, String>();
        param.put("type", "1");
        param.put("phone", phone);
        APIHub.getsInstance().getBaseService().getValidate(param).enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                Result result = response.body();
                if (result.getCode().equals("200")) {
                    TipToast.toast(ForgetPasswordActivity.this, "获取验证码成功！");
                } else {
                    mCountDownTimerUtils.cancel();
                    mCountDownTimerUtils.onFinish();
                    TipToast.toast(ForgetPasswordActivity.this, result.getRemark());
                }
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                mCountDownTimerUtils.cancel();
                mCountDownTimerUtils.onFinish();
                TipToast.toast(ForgetPasswordActivity.this, "网络连接失败");
            }
        });
    }

    @OnClick(R.id.btn_confirm)
    public void onClickConfirm(View view) {
        /*UserInfo userInfo = UserDBHelper.getInstance(this).getUserInfo();
        if (TextUtils.isEmpty(userInfo.getId())) {
            finish();
            return;
        }*/
        String phone = mEtPhoneNum.getText().toString();
        String password = mEtPassword.getText().toString();
        String verityCode = mEtVerityCode.getText().toString();
        if (verifyInput(phone, password, verityCode)) {
            showLoadingDialog();
            Map<String, String> param = new HashMap<String, String>();
//            param.put("id", userInfo.getId());
            param.put("phone", phone);
            param.put("password", password);
            param.put("validate", verityCode);
            APIHub.getsInstance().getBaseService().updateUserInfo(param).enqueue(new Callback<Result>() {
                @Override
                public void onResponse(Call<Result> call, Response<Result> response) {
                    dismissLoadingDialog();
                    Result result = response.body();
                    if (result.getCode().equals("200")) {
                        TipToast.toast(ForgetPasswordActivity.this, "修改密码成功！");
                        Intent intent = new Intent(ForgetPasswordActivity.this, LogInActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                        finish();
                    } else {
                        TipToast.toast(ForgetPasswordActivity.this, result.getRemark().toString());
                    }
                }

                @Override
                public void onFailure(Call<Result> call, Throwable t) {
                    dismissLoadingDialog();
                    TipToast.toast(ForgetPasswordActivity.this, "网络连接失败");
                }
            });
        }
    }

    @OnClick(R.id.iv_back)
    public void clickBack(View view) {
        finish();
    }

    private boolean verifyInput(String phone, String password, String verityCode) {
        if (TextUtils.isEmpty(phone)) {
            TipToast.toast(this, "请输入手机号");
            return false;
        }
        if (!StringUtils.isPhone(phone)) {
            TipToast.toast(this, "请输入正确的手机号");
            return false;
        }
        if (!StringUtils.isPassword(password)) {
            TipToast.toast(this, "请输入6-20位字母数字密码组合");
            return false;
        }
        if (TextUtils.isEmpty(verityCode)) {
            TipToast.toast(this, "请输入验证码");
            return false;
        }
        return true;
    }
}
